﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace _20200304lianxi3
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Click += Button1_Click1;
        }
        public void Button1_Click1(object sender, EventArgs e)
        {
            //1、制作画布
            System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
            Graphics g = Graphics.FromImage(img);
            //水印样式：画什么东西
            string a = "大大的水印！";
            //字体、大小
            Font f = new Font("黑体", 20);
            //颜色
            Brush b = new SolidBrush(Color.Red);
            //0,0——开始画水印的位置
            g.DrawString(a, f, b, 0, 0);
            //保存路径
            string path = "img/" + FileUpload1.FileName;
            img.Save(Server.MapPath(path));
            //在image控件中展示
            ImageButton1.ImageUrl = path;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string filename = FileUpload1.FileName;
            string filenamefix = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
            if (filenamefix == "jpg" || filenamefix == "png")
            {
                FileUpload1.SaveAs(Server.MapPath(".") + "//img//" + filename);
                Response.Write("<script>alert('图片上传成功！')</script>");
                this.ImageButton1.ImageUrl = "/img/" + filename;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (ImageButton1.ImageUrl == "")
            {
                Response.Write("<script>alert('没有可下载的图片，请先上传！')</script>");
            }
            else
            {
                Response.ContentType = "application/x-zip-compressed";
                String imageNmae = ImageButton1.ImageUrl.Substring(ImageButton1.ImageUrl.LastIndexOf('/') + 1).ToLower();
                Response.AddHeader("Content-Disposition", "attachement;filename=" + imageNmae);
                string filename = Server.MapPath(ImageButton1.ImageUrl);
                Response.TransmitFile(filename);
            }
        }
    }
}